package com.example.zhihu.controller;

import com.example.zhihu.pojo.Modular;
import com.example.zhihu.service.ModualService;
import com.example.zhihu.utils.JSONResult;
import com.example.zhihu.utils.PagedResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(value="模块接口", tags = {"模块controller"})
@RestController
@RequestMapping("/api/modular")
public class ModualController {
    @Autowired
    private ModualService modualService;

    @ApiOperation("添加模块")
    @PostMapping("/addModular")
    public JSONResult add(@RequestBody Modular modular) {
        Modular res = modualService.addModual(modular);
        return JSONResult.ok(res);
    }

    @ApiOperation("修改模块")
    @PostMapping("/updateModular")
    public JSONResult edit(@RequestBody Modular modular) {
        if(StringUtils.isBlank(modular.getId())) {
            return JSONResult.errorMsg("修改错误");
        }
        Modular res = modualService.updateModual(modular);
        return JSONResult.ok(res);
    }

    @ApiOperation("显示模块")
    @GetMapping("/queryModular")
    public JSONResult queryModular(@RequestParam(required = false) Integer page) {
        if(page == null) {
            page = 1;
        }
        PagedResult result = modualService.modualList(page,50);
        return JSONResult.ok(result);
    }
}
